<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SystemLog extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'action',
        'module',
        'target_type',
        'target_id',
        'description',
        'old_data',
        'new_data',
        'ip_address',
        'user_agent',
        'level',
        'context',
    ];

    protected $casts = [
        'old_data' => 'array',
        'new_data' => 'array',
        'context' => 'array',
    ];

    /**
     * 关联操作用户
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 检查日志级别是否为信息
     */
    public function isInfo(): bool
    {
        return $this->level === 'info';
    }

    /**
     * 检查日志级别是否为警告
     */
    public function isWarning(): bool
    {
        return $this->level === 'warning';
    }

    /**
     * 检查日志级别是否为错误
     */
    public function isError(): bool
    {
        return $this->level === 'error';
    }

    /**
     * 检查日志级别是否为严重
     */
    public function isCritical(): bool
    {
        return $this->level === 'critical';
    }

    /**
     * 获取日志级别标签
     */
    public function getLevelLabelAttribute(): string
    {
        $levelLabels = [
            'info' => '信息',
            'warning' => '警告',
            'error' => '错误',
            'critical' => '严重',
        ];

        return $levelLabels[$this->level] ?? $this->level;
    }

    /**
     * 获取日志级别颜色
     */
    public function getLevelColorAttribute(): string
    {
        $levelColors = [
            'info' => '#17A2B8',
            'warning' => '#FFC107',
            'error' => '#DC3545',
            'critical' => '#721C24',
        ];

        return $levelColors[$this->level] ?? '#6C757D';
    }

    /**
     * 获取日志级别图标
     */
    public function getLevelIconAttribute(): string
    {
        $levelIcons = [
            'info' => 'info-circle',
            'warning' => 'exclamation-triangle',
            'error' => 'times-circle',
            'critical' => 'exclamation-circle',
        ];

        return $levelIcons[$this->level] ?? 'file-text';
    }

    /**
     * 检查是否为重要日志
     */
    public function isImportant(): bool
    {
        return in_array($this->level, ['warning', 'error', 'critical']);
    }

    /**
     * 获取操作摘要
     */
    public function getActionSummaryAttribute(): string
    {
        $summary = $this->action;
        
        if ($this->target_type && $this->target_id) {
            $summary .= " - {$this->target_type} #{$this->target_id}";
        }
        
        if ($this->module) {
            $summary .= " ({$this->module})";
        }
        
        return $summary;
    }

    /**
     * 获取数据变更摘要
     */
    public function getChangeSummaryAttribute(): string
    {
        if (!$this->old_data && !$this->new_data) {
            return '无数据变更';
        }

        if (!$this->old_data) {
            return '新增数据';
        }

        if (!$this->new_data) {
            return '删除数据';
        }

        return '更新数据';
    }

    /**
     * 记录系统日志
     */
    public static function log(
        string $action,
        string $module,
        string $description,
        ?int $userId = null,
        ?string $targetType = null,
        ?int $targetId = null,
        ?array $oldData = null,
        ?array $newData = null,
        string $level = 'info',
        ?array $context = null
    ): self {
        return static::create([
            'user_id' => $userId,
            'action' => $action,
            'module' => $module,
            'target_type' => $targetType,
            'target_id' => $targetId,
            'description' => $description,
            'old_data' => $oldData,
            'new_data' => $newData,
            'level' => $level,
            'context' => $context,
            'ip_address' => request()->ip(),
            'user_agent' => request()->userAgent(),
        ]);
    }

    /**
     * 记录信息日志
     */
    public static function info(
        string $action,
        string $module,
        string $description,
        ?int $userId = null,
        ?string $targetType = null,
        ?int $targetId = null,
        ?array $oldData = null,
        ?array $newData = null,
        ?array $context = null
    ): self {
        return static::log($action, $module, $description, $userId, $targetType, $targetId, $oldData, $newData, 'info', $context);
    }

    /**
     * 记录警告日志
     */
    public static function warning(
        string $action,
        string $module,
        string $description,
        ?int $userId = null,
        ?string $targetType = null,
        ?int $targetId = null,
        ?array $oldData = null,
        ?array $newData = null,
        ?array $context = null
    ): self {
        return static::log($action, $module, $description, $userId, $targetType, $targetId, $oldData, $newData, 'warning', $context);
    }

    /**
     * 记录错误日志
     */
    public static function error(
        string $action,
        string $module,
        string $description,
        ?int $userId = null,
        ?string $targetType = null,
        ?int $targetId = null,
        ?array $oldData = null,
        ?array $newData = null,
        ?array $context = null
    ): self {
        return static::log($action, $module, $description, $userId, $targetType, $targetId, $oldData, $newData, 'error', $context);
    }

    /**
     * 记录严重日志
     */
    public static function critical(
        string $action,
        string $module,
        string $description,
        ?int $userId = null,
        ?string $targetType = null,
        ?int $targetId = null,
        ?array $oldData = null,
        ?array $newData = null,
        ?array $context = null
    ): self {
        return static::log($action, $module, $description, $userId, $targetType, $targetId, $oldData, $newData, 'critical', $context);
    }
}
